package registnet.records.source;

import grammar.PrerequisiteCruncher;

import java.util.List;
import java.util.Vector;

import registnet.records.DefaultCourse;
import registnet.records.Department;
import registnet.records.IPrerequisite;
import registnet.records.Prerequisite;

public class DefaultCourseSource {

	private String courseCode;
	private DefaultCourse course;
	
	
	/**
	 * Default constructor
	 * 
	 * @param courseCode
	 */
	public DefaultCourseSource(String courseCode){
		this.courseCode = courseCode;
		this.load();
	}
	
	
	/**
	 * Loads data from srouce
	 */
	public void load(){
		try{
			registnet.dao.Course DAOCourse = new registnet.dao.CoursePeer().getCourse(this.courseCode);
			Department department = new DepartmentSource(DAOCourse.getUnit().getUnitCode()).getDepartment();
			this.course = new DefaultCourse(this.courseCode);
			this.course.setCourseName(DAOCourse.getCourseName());
			this.course.setDepartment(department);
			this.course.setOffering(this.getOffering(DAOCourse.getSemesterOffered()));
			this.course.setUnit(DAOCourse.getUnits());
			this.course.setPrerequisites(this.getPrerequisites(DAOCourse.getPrerequisite()));
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Gets the semesters when the course is offered
	 * 
	 * @param semesterOffering - The semesters when the course is offered, in text
	 * @return	The semesters when the course is offered, in an array
	 */
	private boolean[] getOffering(String source){
		boolean[] offering = new boolean[3];
		
		if (source.contains("1")) offering[0] = true;
		else offering[0] = false;
		
		if (source.contains("2")) offering[1] = true;
		else offering[1] = false;
		
		if (source.contains("S")) offering[2] = true;
		else offering[2] = false;
		
		return offering;
	}
	
	
	/**
	 * Gets the prerequisites of the course
	 * 
	 * @param source - The prerequisites of the course, in text
	 * @return	The prerequisites of the course, in list
	 */
	private List<IPrerequisite> getPrerequisites(String source){
		List<IPrerequisite> prerequisites = new Vector<IPrerequisite>();
		List<String> temp = PrerequisiteCruncher.getPrerequisites(source);
		for(int i=0; i<temp.size(); i++){
			Prerequisite prerequisite = new Prerequisite(temp.get(i));
			prerequisites.add(prerequisite);
		}
		return prerequisites;
	}
	
	
	/**
	 * Gets the default course
	 * 
	 * @return	The default course
	 */
	public DefaultCourse getDefaultCourse(){
		return this.course;
	}
	
}
